diff options
| author | Mike Buland <mike@xagasoft.com> | 2015-01-07 09:53:14 -0700 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2015-01-07 09:53:14 -0700 |
| commit | e64cb84fe43660818f7d6eff2954147495b1ac8b (patch) | |
| tree | 8e89978bc4a72677a8728d62eb850d81c9b92d46 | |
| parent | 4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5 (diff) | |
| download | clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.tar.gz clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.tar.bz2 clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.tar.xz clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.zip | |
Fixed parsing bug, added unit tests, debug cleanup.0.12
There was an issue with order of operations outside of parenthesies,
easily solved.
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/number.cpp | 4 | ||||
| -rw-r--r-- | src/number.h | 1 | ||||
| -rw-r--r-- | src/options.cpp | 100 | ||||
| -rw-r--r-- | src/options.h | 1 | ||||
| -rw-r--r-- | src/parser.cpp | 1 | ||||
| -rw-r--r-- | src/unitparser.cpp | 2 |
8 files changed, 110 insertions, 3 deletions
diff --git a/src/config.h b/src/config.h index a3f8b9e..f55be13 100644 --- a/src/config.h +++ b/src/config.h | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #ifndef CONFIG_H | 1 | #ifndef CONFIG_H |
| 2 | #define CONFIG_H | 2 | #define CONFIG_H |
| 3 | 3 | ||
| 4 | #define CLIC_VERSION "0.11" | 4 | #define CLIC_VERSION "0.12" |
| 5 | #define CLIC_VERSION_STR "Clic v" CLIC_VERSION | 5 | #define CLIC_VERSION_STR "Clic v" CLIC_VERSION |
| 6 | 6 | ||
| 7 | #define DEBUG_DIVIDE false | 7 | #define DEBUG_DIVIDE false |
diff --git a/src/main.cpp b/src/main.cpp index a1d7672..1a4ab38 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
| @@ -8,6 +8,8 @@ | |||
| 8 | #include <math.h> | 8 | #include <math.h> |
| 9 | #include <sys/time.h> | 9 | #include <sys/time.h> |
| 10 | 10 | ||
| 11 | #include <bu/file.h> | ||
| 12 | |||
| 11 | #include <bu/sio.h> | 13 | #include <bu/sio.h> |
| 12 | #include <bu/streamstack.h> | 14 | #include <bu/streamstack.h> |
| 13 | using namespace Bu; | 15 | using namespace Bu; |
diff --git a/src/number.cpp b/src/number.cpp index 05e310c..eb803be 100644 --- a/src/number.cpp +++ b/src/number.cpp | |||
| @@ -662,11 +662,11 @@ Number Number::toRadix( int iNewRadix, int iNewScale ) const | |||
| 662 | n.set( iNewRadix ); | 662 | n.set( iNewRadix ); |
| 663 | while( !me.isZero() ) | 663 | while( !me.isZero() ) |
| 664 | { | 664 | { |
| 665 | Bu::println("%1 %% %2 = %3").arg( me ).arg( n ).arg( me%n ); | 665 | // Bu::println("%1 %% %2 = %3").arg( me ).arg( n ).arg( me%n ); |
| 666 | int dig = (me%n).toInt32(); | 666 | int dig = (me%n).toInt32(); |
| 667 | ret.aInt.append( dig ); | 667 | ret.aInt.append( dig ); |
| 668 | me = me / n; | 668 | me = me / n; |
| 669 | Bu::println("New digit: %1 (%2)").arg( dig ).arg( me ); | 669 | // Bu::println("New digit: %1 (%2)").arg( dig ).arg( me ); |
| 670 | } | 670 | } |
| 671 | 671 | ||
| 672 | return ret; | 672 | return ret; |
diff --git a/src/number.h b/src/number.h index fe10788..2f7a583 100644 --- a/src/number.h +++ b/src/number.h | |||
| @@ -49,6 +49,7 @@ public: | |||
| 49 | int getScale() const { return iScale; } | 49 | int getScale() const { return iScale; } |
| 50 | int getEffectiveScale() const; | 50 | int getEffectiveScale() const; |
| 51 | void setScale( int iNewScale ); | 51 | void setScale( int iNewScale ); |
| 52 | int getIntegralDigits() const { return aInt.getSize(); } | ||
| 52 | 53 | ||
| 53 | int32_t toInt32() const; | 54 | int32_t toInt32() const; |
| 54 | Number toRadix( int iNewRadix, int iNewScale=-1 ) const; | 55 | Number toRadix( int iNewRadix, int iNewScale=-1 ) const; |
diff --git a/src/options.cpp b/src/options.cpp index d88df3b..26f0cb7 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
| @@ -39,6 +39,8 @@ Options::Options( int argc, char *argv[] ) : | |||
| 39 | "your shell)."); | 39 | "your shell)."); |
| 40 | addOption( Bu::slot(this, &Options::sum), "sum", | 40 | addOption( Bu::slot(this, &Options::sum), "sum", |
| 41 | "Read numbers from standard input and sum them, output the result."); | 41 | "Read numbers from standard input and sum them, output the result."); |
| 42 | addOption( Bu::slot(this, &Options::grind), 'g', "grind", | ||
| 43 | "Search for magic numbers. Secret magic numbers."); | ||
| 42 | addOption( Bu::slot(this, &Options::version), 'v', "version", | 44 | addOption( Bu::slot(this, &Options::version), 'v', "version", |
| 43 | "Show the version string ('" CLIC_VERSION_STR "')"); | 45 | "Show the version string ('" CLIC_VERSION_STR "')"); |
| 44 | addHelpOption('h', "help", "This help"); | 46 | addHelpOption('h', "help", "This help"); |
| @@ -224,3 +226,101 @@ int Options::version( Bu::StringArray aArgs ) | |||
| 224 | return 0; | 226 | return 0; |
| 225 | } | 227 | } |
| 226 | 228 | ||
| 229 | bool onlyNines( const Bu::String &s ) | ||
| 230 | { | ||
| 231 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
| 232 | { | ||
| 233 | if( *i != '9' ) | ||
| 234 | return false; | ||
| 235 | } | ||
| 236 | return true; | ||
| 237 | } | ||
| 238 | |||
| 239 | bool onlyOnes( const Bu::String &s ) | ||
| 240 | { | ||
| 241 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
| 242 | { | ||
| 243 | if( *i != '1' ) | ||
| 244 | return false; | ||
| 245 | } | ||
| 246 | return true; | ||
| 247 | } | ||
| 248 | |||
| 249 | int Options::grind( Bu::StringArray aArgs ) | ||
| 250 | { | ||
| 251 | Number test("1"); | ||
| 252 | Number bit("1"); | ||
| 253 | Number mult("2"); | ||
| 254 | int64_t iBits = 1; | ||
| 255 | int iLastSize = 0; | ||
| 256 | |||
| 257 | if( aArgs.getSize() > 1 ) | ||
| 258 | { | ||
| 259 | Bu::print("Loading number..."); Bu::sioRaw.flush(); | ||
| 260 | Bu::String num = Bu::File(aArgs[1], Bu::File::Read).readAll(); | ||
| 261 | test = num; | ||
| 262 | Bu::println("done."); | ||
| 263 | Bu::print("Computing bit..."); Bu::sioRaw.flush(); | ||
| 264 | bit = test; | ||
| 265 | bit = bit+Number("1"); | ||
| 266 | bit = bit/mult; | ||
| 267 | Bu::println("done."); | ||
| 268 | if( aArgs.getSize() > 2 ) | ||
| 269 | { | ||
| 270 | Bu::print("Trusting a human for where we are..."); Bu::sioRaw.flush(); | ||
| 271 | iBits = strtoll( aArgs[2].getStr(), NULL, 10 ); | ||
| 272 | } | ||
| 273 | else | ||
| 274 | { | ||
| 275 | Bu::print("Figuring out where we are..."); Bu::sioRaw.flush(); | ||
| 276 | Bu::String sBin = bit.toRadix(2).toString(); | ||
| 277 | iBits = sBin.getSize(); | ||
| 278 | } | ||
| 279 | // Bu::println("Done. Checking."); | ||
| 280 | // Bu::println("test = %1, bit = %2").arg( test ).arg( bit ); | ||
| 281 | // Bu::String sTest = test.toRadix(2).toString(); | ||
| 282 | // Bu::File("check-bit.txt", Bu::File::WriteNew ).write( sBin ); | ||
| 283 | // Bu::File("check-test.txt", Bu::File::WriteNew ).write( sTest ); | ||
| 284 | iLastSize = num.getSize(); | ||
| 285 | // Bu::println("File written. %1 bits").arg( sBin.getSize() ); | ||
| 286 | Bu::println("done."); | ||
| 287 | Bu::println("Continuing calculation..."); | ||
| 288 | } | ||
| 289 | |||
| 290 | for(;;) | ||
| 291 | { | ||
| 292 | bit = bit*mult; | ||
| 293 | test = test + bit; | ||
| 294 | Bu::String r = test.toString(); | ||
| 295 | iBits++; | ||
| 296 | |||
| 297 | if( onlyNines(r) ) | ||
| 298 | { | ||
| 299 | Bu::println("%1 digits").arg( r.getSize() ); | ||
| 300 | Bu::File f("answer-dec.txt", Bu::File::WriteNew ); | ||
| 301 | f.write( r ); | ||
| 302 | Bu::println("Success."); | ||
| 303 | // Bu::println("%1 == %2").arg( test.toRadix(2) ).arg( r ); | ||
| 304 | return 0; | ||
| 305 | } | ||
| 306 | if( r.getSize() != iLastSize && r.getSize()%1000 == 0 ) | ||
| 307 | { | ||
| 308 | /* | ||
| 309 | Bu::String rs = test.toRadix(2).toString(); | ||
| 310 | int iOBits = rs.getSize(); | ||
| 311 | if( iOBits != iBits ) | ||
| 312 | Bu::println("Bit mismatch: %1 vs %2").arg( iOBits ).arg( iBits ); | ||
| 313 | if( !onlyOnes( rs ) ) | ||
| 314 | Bu::println("Not only ones!"); | ||
| 315 | */ | ||
| 316 | iLastSize = r.getSize(); | ||
| 317 | Bu::println("%1 digits").arg( r.getSize() ); | ||
| 318 | Bu::File f(Bu::String("interum-%1-%2.txt").arg( r.getSize() ).arg( iBits ), Bu::File::WriteNew ); | ||
| 319 | f.write( r ); | ||
| 320 | } | ||
| 321 | } | ||
| 322 | Bu::println("Failure."); | ||
| 323 | exit( 0 ); | ||
| 324 | return 0; | ||
| 325 | } | ||
| 326 | |||
diff --git a/src/options.h b/src/options.h index f3f59a6..a7453ab 100644 --- a/src/options.h +++ b/src/options.h | |||
| @@ -20,6 +20,7 @@ private: | |||
| 20 | int execute( Bu::StringArray aArgs ); | 20 | int execute( Bu::StringArray aArgs ); |
| 21 | int sum( Bu::StringArray aArgs ); | 21 | int sum( Bu::StringArray aArgs ); |
| 22 | int version( Bu::StringArray aArgs ); | 22 | int version( Bu::StringArray aArgs ); |
| 23 | int grind( Bu::StringArray aArgs ); | ||
| 23 | 24 | ||
| 24 | int iScale; | 25 | int iScale; |
| 25 | int iRadix; | 26 | int iRadix; |
diff --git a/src/parser.cpp b/src/parser.cpp index bd49c38..153b6fe 100644 --- a/src/parser.cpp +++ b/src/parser.cpp | |||
| @@ -270,6 +270,7 @@ void Parser::unwind() | |||
| 270 | { | 270 | { |
| 271 | 271 | ||
| 272 | tsNonTerminal.pop(); | 272 | tsNonTerminal.pop(); |
| 273 | unwind(); | ||
| 273 | } | 274 | } |
| 274 | else | 275 | else |
| 275 | { | 276 | { |
diff --git a/src/unitparser.cpp b/src/unitparser.cpp index e23f76c..f518fdc 100644 --- a/src/unitparser.cpp +++ b/src/unitparser.cpp | |||
| @@ -38,6 +38,8 @@ void UnitParser::order1() | |||
| 38 | unitTest(parse("1.59*40/24*21", 5) == "55.65"); | 38 | unitTest(parse("1.59*40/24*21", 5) == "55.65"); |
| 39 | unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); | 39 | unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); |
| 40 | unitTest(parse("10+2*2*2+2") == "20"); | 40 | unitTest(parse("10+2*2*2+2") == "20"); |
| 41 | unitTest(parse("5-5-1") == "-1"); | ||
| 42 | unitTest(parse("5-(1+2+2)-1") == "-1"); | ||
| 41 | } | 43 | } |
| 42 | 44 | ||
| 43 | void UnitParser::assignment() | 45 | void UnitParser::assignment() |
